/************************************************************************
 * @file AbstractLogCommand.cpp
 * @author Ian Chen
 * @version 1.0
 * 
 * @section LICENSE
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
 * General Public License for more details at
 * http://www.gnu.org/licenses/lgpl.html
 ************************************************************************/
#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QStringBuilder>

#include <plugin/command/AbstractLogCommand.h>

namespace antu
{
namespace plugin
{
AbstractLogCommand::AbstractLogCommand()
    : AbstractCommand(LoggerService::LOG_COMMAND_NAME),
      m_eLogType(LoggerService::LVL_DEBUG)
{
    QDir dir(qApp->applicationDirPath());
    QString filepath = dir.absoluteFilePath("log");

    if (!dir.exists(filepath))
    {
        dir.mkdir("log");
    }

    SET_COMMAND_PARAM_SETTING(LoggerService::LOG_PARAM_TYPE, QMetaType::Int);
    SET_COMMAND_PARAM_SETTING(LoggerService::LOG_PARAM_FILE, QMetaType::QString);
    SET_COMMAND_PARAM_SETTING(LoggerService::LOG_PARAM_LINE, QMetaType::Int);
    SET_COMMAND_PARAM_SETTING(LoggerService::LOG_PARAM_CLASS, QMetaType::QString);
    SET_COMMAND_PARAM_SETTING(LoggerService::LOG_PARAM_METHOD, QMetaType::QString);
    SET_COMMAND_PARAM_SETTING(LoggerService::LOG_PARAM_MESSAGE, QMetaType::QString);
}

// executing the actual command
AbstractCommand::CommandReturnType AbstractLogCommand::executeCommand(CommandParameters&, const CommandParameters& inputs, CommandParameters&)
{
    int type;// = inputs.at(LoggerService::LOG_PARAM_TYPE).toInt();
    ConstCommandParameterIterator iter = inputs.find(LoggerService::LOG_PARAM_TYPE);
    type = iter->second.toInt();

    QString fileName;
    iter = inputs.find(LoggerService::LOG_PARAM_FILE);
    fileName = iter->second.toString();

    int line;
    iter = inputs.find(LoggerService::LOG_PARAM_LINE);
    line = iter->second.toInt();

    QString className;// = inputs[LoggerService::LOG_PARAM_CLASS].toString();
    iter = inputs.find(LoggerService::LOG_PARAM_CLASS);
    className = iter->second.toString();

    QString methodName;// = inputs[LoggerService::LOG_PARAM_METHOD].toString();
    iter = inputs.find(LoggerService::LOG_PARAM_METHOD);
    methodName = iter->second.toString();

    QString message;// = inputs[LoggerService::LOG_PARAM_MESSAGE].toString();
    iter = inputs.find(LoggerService::LOG_PARAM_MESSAGE);
    message = iter->second.toString();

    switch (type)
    {
    case LoggerService::LVL_DEBUG:
        this->debug(fileName, line, className, methodName, message);
        break;

    case LoggerService::LVL_INFO:
        this->info(fileName, line, className, methodName, message);
        break;

    case LoggerService::LVL_WARNING:
        this->warn(fileName, line, className, methodName, message);
        break;

    case LoggerService::LVL_ERROR:
        this->error(fileName, line, className, methodName, message);
        break;

    case LoggerService::LVL_FATAL:
        this->fatal(fileName, line, className, methodName, message);
        break;

    default:
        this->debug(fileName, line, className, methodName, message);
        break;
    }

    return AbstractCommand::Success;
}

void AbstractLogCommand::log( LoggerService::LogType type, const QString& fileName, int line, const QString& className, const QString& methodName, const QString& message )
{
    switch (type)
    {
    case LoggerService::LVL_DEBUG:
        this->debug(fileName, line, className, methodName, message);
        break;
    case LoggerService::LVL_ERROR:
        this->error(fileName, line, className, methodName, message);
        break;
    case LoggerService::LVL_FATAL:
        this->fatal(fileName, line, className, methodName, message);
        break;
    case LoggerService::LVL_INFO:
        this->info(fileName, line, className, methodName, message);
        break;
    case LoggerService::LVL_WARNING:
        this->warn(fileName, line, className, methodName, message);
        break;
    }
}

LoggerService::LogType AbstractLogCommand::logLevel() const
{
    return this->m_eLogType;
}
}
}
